package com.cuit.controller;

import com.alibaba.fastjson.JSON;
import com.cuit.pojo.PhoneCode;
import com.cuit.pojo.User;
import com.cuit.result.R;
import com.cuit.service.FileService;
import com.cuit.service.SMSService;
import com.cuit.service.UserService;
import com.cuit.utils.HashUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author chishuiLi     Email:1192537447@qq.com
 * @Description
 */
@CrossOrigin  //跨域
@Api(tags = "登录页")
@RequestMapping("/login")
@RestController
public class LoginController {

    @Resource
    private FileService fileService;

    @Resource
    private UserService userService;

    @Resource
    private SMSService smsService;

    @Resource
    private RedisTemplate<String,String> rt;


    @GetMapping("/isExist/{username}")
    @ApiOperation("账号测试有无此用户")
    public R isExistUser(@PathVariable("username") String username){
        HashMap<String, Object> map = new HashMap<>();
        map.put("username",username);
        //用户名无重复，只会查出一个用户
        List<User> userList = userService.listByMap(map);
        //若没有该用户
        if(userList.size() == 0){
            return R.error().data("errMsg","无此用户");
        }else{
            User user = userList.get(0);
            //得到全路径
            String imgPath = fileService.allPath(user.getAvatar());
            return R.ok().data("imgPath",imgPath);
        }
    }
    @GetMapping("/isUserPhone/{phone}")
    @ApiOperation("手机号测试有无此用户")
    public R isUserPhone(@PathVariable("phone") String phone){
        HashMap<String, Object> map = new HashMap<>();
        map.put("phone",phone);
        //电话号码无重复，只会查出一个用户
        List<User> userList = userService.listByMap(map);
        //若没有该用户
        if(userList.size() == 0){
            return R.error().data("errMsg","无此用户");
        }else{
            User user = userList.get(0);
            //得到全路径
            String imgPath = fileService.allPath(user.getAvatar());
            return R.ok().data("imgPath",imgPath);
        }
    }

    @RequestMapping("/toLogin")
    @ApiOperation("账号密码登录接口测试")
    public R toLogin(@RequestBody User user){

//        User user = JSON.parseObject(userStr, User.class);

        HashMap<String, Object> map = new HashMap<>();
        map.put("username",user.getUsername());
        map.put("password", HashUtils.encryPassword(user.getPassword()));
        List<User> userList = userService.listByMap(map);
        if(userList.size() == 0){
            return R.error().data("errMsg","密码错误");
        }
        User selectUser = userList.get(0);
        //屏蔽敏感信息
        selectUser.setPassword("********");
        //图片处理
        selectUser.setAvatar(fileService.allPath(selectUser.getAvatar()));

        return R.ok().data("user",selectUser);
    }
    @RequestMapping("/toLoginByPhone")
    @ApiOperation("手机验证码登录接口测试")
    public R toLoginByPhone(@RequestBody String phoneCodeStr){

        PhoneCode phoneCode = JSON.parseObject(phoneCodeStr, PhoneCode.class);

        if(!smsService.phoneCodeIsSuccess(phoneCode.getPhone(),phoneCode.getCode())){
            return R.error().data("errMsg","验证码错误");
        }else {
            HashMap<String, Object> map = new HashMap<>();
            map.put("phone",phoneCode.getPhone());
            List<User> userList = userService.listByMap(map);
            if(userList.size() == 0){
                return R.error().data("errMsg","无此用户");
            }
            User selectUser = userList.get(0);
            //屏蔽敏感信息
            selectUser.setPassword("********");
            //图片处理
            selectUser.setAvatar(fileService.allPath(selectUser.getAvatar()));

            return R.ok().data("user",selectUser);
        }
    }
}
